package com.liuda.commons.filter;

import com.liuda.commons.constants.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ControllerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute(Constants.SESSION_USER)!=null){
            filterChain.doFilter(request,servletResponse);
        }else{
            HttpServletResponse response=(HttpServletResponse) servletResponse;
            response.sendRedirect(request.getContextPath()+"/settings/user/toLogin.try");
        }
    }

    @Override
    public void destroy() {

    }
}
